/*
 * Copyright (c) 2015, 张涛.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kymjs.themvp.presenter;

import com.kymjs.themvp.view.IDelegate;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.PacMap;

/**
 * Presenter base class for Activity
 * Presenter层的实现基类
 *
 * @param <T> View delegate class type
 * @author kymjs (http://www.kymjs.com/) on 10/23/15.
 */
public abstract class ActivityPresenter<T extends IDelegate> extends Ability {
    /**
     * LabelLog
     */
    public static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, "[theMvp] ");
    /**
     * viewDelegate
     */
    protected T viewDelegate;

    /**
     * ActivityPresenter
     */
    public ActivityPresenter() {
        try {
            viewDelegate = getDelegateClass().newInstance();
        } catch (Exception e) {
            HiLog.warn(LABEL_LOG, e.toString());
        }
    }

    /**
     * onStart
     *
     * @param intent intent
     */
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        viewDelegate.create(this, null, false);
        setUIContent(viewDelegate.getRootView());
        initToolbar();
        viewDelegate.initWidget();
        bindEvenListener();
    }

    /**
     * bindEvenListener
     */
    protected void bindEvenListener() {
    }

    /**
     * initToolbar
     */
    protected void initToolbar() {
    }

    /**
     * onRestoreAbilityState
     *
     * @param inState inState
     */
    @Override
    public void onRestoreAbilityState(PacMap inState) {
        super.onRestoreAbilityState(inState);
        if (viewDelegate == null) {
            try {
                viewDelegate = getDelegateClass().newInstance();
            } catch (Exception e) {
                HiLog.warn(LABEL_LOG, e.toString());
            }
        }
    }


    /**
     * onStop
     */
    @Override
    protected void onStop() {
        super.onStop();
        viewDelegate = null;
    }

    /**
     * getDelegateClass
     *
     * @return class
     */
    protected abstract Class<T> getDelegateClass();
}
